package com.sdgakj.service.family;

import com.google.common.collect.Maps;
import com.sdgakj.dto.FeedDTO;
import com.sdgakj.entity.*;
import com.sdgakj.repository.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;


@Component
@Transactional
public class FamilyService {
	
	@Autowired
	private FamilyDao familyDao;
	@Autowired
	private ActivesDao activesDao;
	@Autowired
	private FstatisticsDao fstatisticsDao;
	@Autowired
	private FamilyBookDao familyBookDao;
	@Autowired
	private FamilyPersonDao familyPersonDao;

	private static Logger logger =LoggerFactory.getLogger(FamilyService.class);


	/**
	 * 增加familyPerson
	 * */

	public FamilyPerson saveFamilyPerson(FamilyPerson familyPerson){
		return 	familyPersonDao.save(familyPerson);
	}

	/*
	* 查询所有家族
	* */
	public List<Family> familyList(){
		return (List<Family>) familyDao.findAll();
	}

	/**
	 * 查询家族列表
	 * @param searchParams
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
     * @return
     */
	public Page<Family> getFamilis(Map<String,Object> searchParams, int pageNumber, int pageSize,
								   String sortType){
		PageRequest pageRequest =buildPageRequest(pageNumber,pageSize,sortType);
		Specification<Family> spec =buildSpecification(searchParams);
        Page<Family> families = familyDao.findAll(spec, pageRequest);
        return families;
	}
	/**
	 * controller 关于家族圈
	 * @param searchParams
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @return
	 */
	public Page<Actives> getfamilycircleByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
									   String sortType) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "createtime");
		// Specification<Truck> spec = buildSpecification(searchParams);
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<Actives> spec = DynamicSpecifications.bySearchFilter(filters.values(), Actives.class);
		return activesDao.findAll(spec, pageRequest);
	}

	/**
	 * controller 关于家族数据世代
	 * @param searchParams
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @return
	 */


	public Page<Fstatistics> getfamilydateByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
											   String sortType) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "fId");

		// Specification<Truck> spec = buildSpecification(searchParams);
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<Fstatistics> spec = DynamicSpecifications.bySearchFilter(filters.values(), Fstatistics.class);
		return fstatisticsDao.findAll(spec, pageRequest);
	}


	public Fstatistics getFstatisticsList(Long fId) {
		return  fstatisticsDao.findOne(fId);
	}



	/**
	 * controller 关于加入家族familyjoin
	 * @param searchParams
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @return
	 */

	public Page<Family> getfamilyjoinByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
											   String sortType) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "createtime");

		// Specification<Truck> spec = buildSpecification(searchParams);
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<Family> spec = DynamicSpecifications.bySearchFilter(filters.values(), Family.class);
		return familyDao.findAll(spec, pageRequest);
	}
/*	getfamilyfamouspById
*
* */



	/**
	 * 创建排序.
	 */
	private Sort buildSortRequest(String sortName,String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}else if("desc".equals(sortType)){
			sort = new Sort(Direction.DESC, sortName);
		}else{
			sort = new Sort(Direction.ASC, sortName);
		}
		return sort;
	}
	
	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}

		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}
	
	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Family> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		//  根据用户类型设置查询条件
//		filters.put("user.type", new SearchFilter("user.type", Operator.EQ, EnumUserType.CUSTOMOR.getCode()));
		Specification<Family> spec = DynamicSpecifications.bySearchFilter(filters.values(),Family.class);
		return spec;
	}

	public Family getFmilyById(Long id) {
		return familyDao.findOne(id);
	}

	public Family getFamilyByFamilyName(String fName) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if(StringUtils.isNotBlank(fName)){
			filters.put("fName", new SearchFilter("fName", SearchFilter.Operator.EQ, fName)); //父节点
		}
		Specification<Family> spec = DynamicSpecifications.bySearchFilter(filters.values(), Family.class);
		return familyDao.findOne(spec);
	}

	public Family updateFamily(Family entity) {
		return familyDao.save(entity);

	}

	public Family getFamilyById(Long id) {
		return familyDao.findOne(id);
	}

	public List<FamilyPerson> getFamilysByPersonId(Long personId) {

		return familyPersonDao.getFamilyByFpId(personId);
	}
	/*
	* 家族人员集合
	* @param id 家族id
	* */
	public  List<FamilyPerson> getPersonByFfId(Long id){
		return familyPersonDao.getPersonByFfId(id);
	}
	public  List<FamilyPerson> getPersonByFfIdAndState(Long id){
		return familyPersonDao.getPersonByFfIdAndState(id);
	}

	/**/
	public FamilyPerson getPersonByFIdAndPId(Long familyId,Long personId){
		return familyPersonDao.getPersonByFIdAndPId(familyId,personId);
	}


	public Family saveFamily(Family family) {

		return familyDao.save(family);
	}


	public void deleteFamily(Long id) {
		familyDao.delete(id);
	}

    /**
     * 根据用户id获取家族
     * @param id
     * @return
     */
    public Family getFamilyByUserId(Long id) {
        List<Family> familys = familyDao.getFamilyByUserId(id);
		return  familys.isEmpty()?null:familys.get(0);
    }

	/**
	 * 显示家谱
	 * @param
	 * @return
	 */
	public Page<Familybook> getfamilygenealogyByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
											String sortType) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "id");
		// Specification<Truck> spidec = buildSpecification(searchParams);
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<Familybook> spec = DynamicSpecifications.bySearchFilter(filters.values(), Familybook.class);
		return familyBookDao.findAll(spec, pageRequest);
	}

	public List<Family> getFamilyAll(){
		return (List<Family>) familyDao.findAll();
	}
	public Long getFamilyCount(){
		return  familyDao.getFamilyCount();
	}

	/**
	 * 通过 ffid查询家族所有的人
	 * */

	public  Integer getPersonByFfIdangeneration(Long fpid,Long genumber){
		return familyPersonDao.getPersonByFfIdangeneration(fpid,genumber);
	}
	/**
	 * 通过 fid查询家族世代familyperson
	 * */
    public List<FamilyPerson> getPersongenerationByfid(Long fid){
		return  familyPersonDao.getPersongenerationByfid(fid);
	}
	public void deleteFamilyPerson(FamilyPerson familyPerson){
		familyPersonDao.delete(familyPerson);
	}
	/*
	* 通过家族id查询家族总人数
	* */
	public List<FamilyPerson> getCountByfid(Long fId){
		return familyPersonDao.getCountByfid(fId);
	}
	/*查询世系*/
	public List<Integer>  getPersonGeneration(Long familyId){
		return  familyPersonDao.getPersonGeneration(familyId);
	}

}
